﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
namespace syc.Store
{
    public partial class 废料到发登记管理 : System.Web.UI.Page
    {
        private string RowColor = "aaff66";
        protected HSSFWorkbook hssfworkbook;
        protected double 废车削;
        protected double 废打包带;
        protected double 废切头;

        protected double 废车削基数;
        protected double 废打包带基数;
        protected double 废切头基数;

        protected int 一分厂套管入库支数;
        protected double 一分厂套管理论重量;

        protected int 二分厂套管入库支数;
        protected double 二分厂套管理论重量;

        protected int 二分厂油管入库支数;
        protected double 二分厂油管理论重量;

        protected int 五分厂油管入库支数;
        protected double 五分厂油管理论重量;

        protected int 三分厂套管接箍数;
        protected double 三分厂套管接箍理论重量;

        protected int 三分厂油管接箍数;
        protected double 三分厂油管接箍理论重量;


        protected double 废车削计划单价;
        protected double 废打包带计划单价 ;
        protected double 废切头计划单价;

        protected double 废车削实际单价;
        protected double 废打包带实际单价;
        protected double 废切头实际单价;

        protected double 一分厂废打包带实际重量;
        protected double 二分厂废打包带实际重量;
        protected double 五分厂废打包带实际重量;
        protected double 四分厂废打包带实际重量;


        protected string 起始日期;
        protected string 截止日期;

        protected string Notice = "";
        int lastmid = 0;
        string sql =
@"SELECT t1.ID
,t1.MID
,t1.废料ID,
t2.废料名称,
t2.规格
,t3.名称 as 供货或使用
,t1.日期
,cast(发货 as tinyint) as 到发
,cast(abs(t1.数量) as float) as 数量
,t1.单价
,t1.单价*t1.数量 as 金额
,t1.保管员
,t1.单据号
,t1.日期 as 入出库日期
,t1.结算方式
FROM 废料到发记录 t1
left outer join 废料 t2 on t2.ID=t1.废料ID
left outer join 废料收货单位 t3 on t3.ID=t1.单位ID
left outer join 生产单位 t4 on t4.ID=t1.入库单位ID
where t1.发货=1
";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.LeiBie.DataBind();
                this.ShouHuoDanWei.DataBind();              
                this.Button_Out.Enabled = false;
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
            this.Panel2.Visible = true;
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.Button_Edit.Enabled = false;
            this.Button_Edit0.Enabled = false;
            this.Button_Delete.Enabled = false;
            this.Button_Look.Enabled = false;
            this.Button_Out.Enabled = false;

            this.GridView1.SelectedIndex = -1;

            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {     
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Delete.Enabled = true;
            this.Button_Look.Enabled = true;
            this.Button_Edit.Enabled = true;
            this.Button_Edit0.Enabled = true;
            string list = this.Button_Delete.CommandName;
            string result = Direct(list);
            string 单据号 = "0";

            if (result == "False")
            {
                if (单据号 == "")
                {
                    this.Button_In.Enabled = true;
                    
                }
                else
                {
                    this.Button_In.Enabled = true;                    
                }
                this.Button_Edit0.Visible = true;
                this.Button_Edit.Visible = false;
                this.Button_Out.Enabled = false;
                if (this.Panel3.Visible == true)
                {
                    this.Panel2.Visible = false;
                }
                if (this.Panel3.Visible == false)
                {
                    this.Panel2.Visible = true;
                }
                
            }
            else
            {
                if (单据号 == "0")
                {
                    this.Button_Out.Enabled = true;
                }
                else
                {
                    this.Button_Out.Enabled = false;
                }
                this.Button_In.Enabled = true;
                this.Button_Edit0.Visible = false;
                this.Button_Edit.Visible = true;
                if (this.Panel3.Visible == true)
                {
                    this.Panel2.Visible = false;
                }
                if (this.Panel3.Visible == false)
                {
                    this.Panel2.Visible = true;
                }
               // ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料发货登记.aspx?id=" + list + "',600,300);</script>");
            }
          

        
        }
        private string GetState()
        {
            string state = "";

            if (this.LeiBie.SelectedItem.Value != "0")
            {
                state += " and t1.废料ID=" + this.LeiBie.SelectedItem.Value;

            }
           
            if (this.ShouHuoDanWei.Text != "0")
            {
                if (this.ShouHuoDanWei.SelectedItem.Text == "一分厂" || this.ShouHuoDanWei.SelectedItem.Text == "二分厂" || this.ShouHuoDanWei.SelectedItem.Text == "三分厂" || this.ShouHuoDanWei.SelectedItem.Text == "四分厂" || this.ShouHuoDanWei.SelectedItem.Text == "五分厂")
                {
                    state += " and t4.ID=" + this.ShouHuoDanWei.Text;
                }
                else
                {
                    state += " and t3.ID=" + this.ShouHuoDanWei.Text;
                }
            }     

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

            //if (state.StartsWith(" and"))
            //{
            //    state = state.Substring(4);
            //}
            //if (state != "")
            //{
            //    state = " where " + state;
            //}
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string state = this.GetState();

            this.SqlText.Text = sql + state + " order by t1.日期 desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.LeiBie.Text = "0";       
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[1].ToString();
            this.SqlDataSource1.DeleteParameters["MID"].DefaultValue = this.Button_Delete.CommandName;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

      

        protected void Button_In_Click(object sender, EventArgs e)
        {
            string list = this.Button_Delete.CommandName;
            if (list == "")
            {
                this.Notice = "请选择到货的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料入库_In.aspx?list=" + list + "',770,440);</script>");
            }
        }

        protected void Button_Out_Click(object sender, EventArgs e)
        {
            string list = this.Button_Delete.CommandName;
            if (list == "")
            {
                this.Notice = "请选择出库的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料调拨单.aspx?id=" + list + "',860,440);</script>");
            }
        }

        protected void Button_In_Edit_Click(object sender, EventArgs e)
        {
            if (this.Panel3.Visible == true)
            {
                this.Panel2.Visible = false;
            }
            if (this.Panel3.Visible == false)
            {
                this.Panel2.Visible = true;
            }
            string list = this.Button_Delete.CommandName;
            string DID = this.GridView1.SelectedDataKey[1].ToString();
            ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料入库单.aspx?id=" + list + "&DID=" + DID + "',685,410);</script>");
           
        }

        protected void Button_Out_Edit_Click(object sender, EventArgs e)
        {
            if (this.Panel3.Visible == true)
            {
                this.Panel2.Visible = false;
            }
            if (this.Panel3.Visible == false)
            {
                this.Panel2.Visible = true;
            }
            string list = this.Button_Delete.CommandName;
            string DID = this.GridView1.SelectedDataKey[1].ToString();
            ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料调拨单.aspx?id=" + list + "&DID=" + DID + "',685,410);</script>");
        }

        protected string Direct(string list)
        {
            string result;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select 发货 from 废料到发记录 where ID=" + list, Conn);
            sda.Fill(ds);
            result = ds.Tables[0].Rows[0][0].ToString();
            return result;
        }       

        protected void Button_To_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
            this.Panel4.Visible = true;
        }
        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("报表/模板/废品分配.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "废品分配";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "废品分配";
            hssfworkbook.SummaryInformation = si;
        }
        protected void WriteToFile()
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + DateTime.Today.ToShortDateString() + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }
        protected void Button_Query_Click(){
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            string sql= @"
            declare @废车削 float
            declare @废打包带 float
            declare @废切头 float          
      
            declare @d1 datetime
            declare @d2 datetime
            select @d1=起始日期,@d2=截止日期 from 生产月设置 where 分厂=0 and 年度=@年度 and 月份=@月份
            select @废车削=isnull(sum(数量),0) from 废料到发记录 where 废料ID=1 and 日期 between @d1 and @d2
            select @废打包带=isnull(sum(数量),0) from 废料到发记录 where 废料ID=2 and 日期 between @d1 and @d2
            select @废切头=isnull(sum(数量),0) from 废料到发记录 where 废料ID=3 and 日期 between @d1 and @d2
            select @废车削,@废打包带,@废切头,@d1,@d2          
           ";
            sql = sql.Replace("@年度",this .NianDu.Text);
            sql = sql.Replace("@月份",this.YueFen.Text);
            Cmd.CommandText = sql;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                废车削 = Reader.GetDouble(0);
                废打包带 = Reader.GetDouble(1);
                废切头 = Reader.GetDouble(2);
                if (Reader.IsDBNull(3))
                {
                    起始日期 = "";
                }
                else
                {
                    起始日期 = Reader.GetDateTime(3).ToString("yyyy年MM月dd日");
                }
                if (Reader.IsDBNull(4))
                {
                    截止日期 = "";
                }
                else
                {
                    截止日期 = Reader.GetDateTime(4).ToString("yyyy年MM月dd日");       
                }
               
            }
            Reader.Close();
            //
            string sql1 = @"
            declare  @一分厂套管入库支数 int
            declare  @一分厂套管N80入库支数 int

            declare  @二分厂套管入库支数 int

            declare @二分厂油管入库支数 int
            declare @五分厂油管入库支数 int

            declare  @三分厂油管接箍数 int
            declare  @三分厂套管接箍数 int      
      
            declare @d1 datetime
            declare @d2 datetime
            select @d1=起始日期,@d2=截止日期 from 生产月设置 where 分厂=0 and 年度=@年度 and 月份=@月份
            select @一分厂套管入库支数=isnull(sum(支数),0) from 产品库存 where 产品类别ID=2 and 生产线 between 100 and 199 and 入库日期 between @d1 and @d2
            select @二分厂油管入库支数=isnull(sum(支数),0) from 产品库存 where 产品类别ID=1 and 生产线 between 200 and  299 and 入库日期 between @d1 and  @d2
            select @二分厂套管入库支数=isnull(sum(支数),0) from 产品库存 where 产品类别ID=2 and 生产线 between 200 and  299 and 入库日期 between @d1 and  @d2
            select @五分厂油管入库支数=isnull(sum(支数),0) from 产品库存 where 产品类别ID=1 and 生产线 between 500 and 599 and 入库日期 between @d1 and  @d2
            select @一分厂套管N80入库支数=isnull(sum(支数),0) from 产品库存 where 产品类别ID=4 and 生产线 between 100 and 199 and 入库日期 between @d1 and @d2
            select @三分厂油管接箍数=isnull(SUM(支数合计),0) from 自产接箍判定单 where 支数合计>0 and 合格=1 and 产品类别ID=1
            select @三分厂套管接箍数=isnull(SUM(支数合计),0) from 自产接箍判定单 where 支数合计>0 and 合格=1 and 产品类别ID=2
     select @一分厂套管入库支数,@二分厂套管入库支数,@二分厂油管入库支数,@五分厂油管入库支数,@一分厂套管N80入库支数,@三分厂套管接箍数,@三分厂油管接箍数       
           ";
            sql1 = sql1.Replace("@年度", this.NianDu.Text);
            sql1 = sql1.Replace("@月份", this.YueFen.Text);
            Cmd.CommandText = sql1;
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                一分厂套管入库支数 = Reader.GetInt32(0) + Reader.GetInt32(4);
                二分厂套管入库支数 = Reader.GetInt32(1);
                二分厂油管入库支数 = Reader.GetInt32(2);
                五分厂油管入库支数 = Reader.GetInt32(3);
                三分厂套管接箍数 = Reader.GetInt32(5);
                三分厂油管接箍数 = Reader.GetInt32(6);
            }
            Reader.Close();
            string sql2 = @"
            declare @废车削计划单价 float
            declare @废打包带计划单价 float
            declare @废切头计划单价 float    
            
            declare @废车削实际单价 float
            declare @废打包带实际单价 float
            declare @废切头实际单价 float          
      
       
            select @废车削计划单价=isnull(计划单价,0),@废车削实际单价=isnull(实际单价,0) from 废料季度招标单价 where 废料ID=1 and 年度=@年度 and 季度=@季度
            select @废打包带计划单价=isnull(计划单价,0),@废打包带实际单价=isnull(实际单价,0) from 废料季度招标单价 where 废料ID=2 and 年度=@年度 and 季度=@季度
            select @废切头计划单价=isnull(计划单价,0),@废切头实际单价=isnull(实际单价,0) from 废料季度招标单价 where 废料ID=3 and 年度=@年度 and 季度=@季度
            select @废车削计划单价,@废打包带计划单价,@废切头计划单价,@废车削实际单价,@废打包带实际单价,@废切头实际单价           
           ";
            sql2 = sql2.Replace("@年度", this.NianDu.Text.ToString());
            int 季度;

            if (this.YueFen.SelectedItem.Value == "1" || this.YueFen.SelectedItem.Value == "2" || this.YueFen.SelectedItem.Value == "3")
            {
                季度 = 1;
                sql2 = sql2.Replace("@季度", 季度.ToString());
            }
            if (this.YueFen.SelectedItem.Value == "4" || this.YueFen.SelectedItem.Value == "5" || this.YueFen.SelectedItem.Value == "6")
            {
                季度 = 2;
                sql2 = sql2.Replace("@季度", 季度.ToString());
            }
            if (this.YueFen.SelectedItem.Value == "7" || this.YueFen.SelectedItem.Value == "8" || this.YueFen.SelectedItem.Value == "9")
            {
                季度 = 3;
                sql2 = sql2.Replace("@季度", 季度.ToString());
            }
            if (this.YueFen.SelectedItem.Value == "10" || this.YueFen.SelectedItem.Value == "11" || this.YueFen.SelectedItem.Value == "12")
            {
                季度 = 4;
                sql2 = sql2.Replace("@季度", 季度.ToString());
            }
            Cmd.CommandText = sql2;
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                if (Reader.IsDBNull(0))
                {
                    废车削计划单价 = 0;
                }
                else
                {
                    废车削计划单价 = Reader.GetDouble(0);
                }
                if (Reader.IsDBNull(1))
                {
                    废打包带计划单价 = 0;
                }
                else
                {
                    废打包带计划单价 = Reader.GetDouble(1);
                }
                if (Reader.IsDBNull(2))
                {
                    废切头计划单价 = 0;
                }
                else
                {
                    废切头计划单价 = Reader.GetDouble(2);
                }

                if (Reader.IsDBNull(3))
                {
                    废车削实际单价 = 0;
                }
                else
                {
                    废车削实际单价 = Reader.GetDouble(3);
                }
                if (Reader.IsDBNull(4))
                {
                    废打包带实际单价 = 0;
                }
                else
                {
                    废打包带实际单价 = Reader.GetDouble(4);
                }
                if (Reader.IsDBNull(5))
                {
                    废切头实际单价 = 0;
                }
                else
                {
                    废切头实际单价 = Reader.GetDouble(5);
                }
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                int mid = (int)this.GridView1.DataKeys[e.Row.RowIndex][1];
                if (mid != lastmid)
                {
                    lastmid = mid;
                    if (this.RowColor == "#aaff66")
                    {
                        this.RowColor = "#aa66ff";
                    }
                    else
                    {
                        this.RowColor = "#aaff66";
                    }
                }
                e.Row.Cells[0].BackColor = System.Drawing.Color.FromName(this.RowColor);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = true;
            this.Panel3.Visible = false;
            this.Panel4.Visible = false;
          
        }
       
        protected void Button_Look_Click(object sender, EventArgs e)
        {
            if (this.Panel3.Visible == true)
            {
                this.Panel2.Visible = false;
            }
            if (this.Panel3.Visible == false)
            {
                this.Panel2.Visible = true;
            }
            string list = this.Button_Delete.CommandName;
            string DID = this.GridView1.SelectedDataKey[1].ToString();
            ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/查看调拨单.aspx?id=" + list + "&DID=" + DID + "',685,370);</script>");
        }
       
    }
}
